package au.com.handsunited.model.validators.entity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

import au.com.handsunited.model.Project;
import au.com.handsunited.model.Project;
import au.com.handsunited.model.validators.address.ProjectLocationValidator;
import au.com.handsunited.model.validators.availability.AvailabilityValidator;
import au.com.handsunited.model.validators.contactinfo.ProjectContactInfoValidator;

/**
 * @author arunda
 *
 */
public class ProjectValidator extends BaseEntityValidator {



	private static Logger logger = LoggerFactory.getLogger(ProjectValidator.class);

	public ProjectValidator (ProjectLocationValidator locationValidator, ProjectContactInfoValidator contactInfoValidator, AvailabilityValidator availabilityValidator) {
		super(locationValidator, contactInfoValidator, availabilityValidator);

	}

	/**
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@Override
	public boolean supports(Class clazz) {
		return clazz.isAssignableFrom(Project.class);
	}

	/**
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	public void validate(Object target, Errors errors) {


		Project project = (Project) target;

		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "project.name.required", "Project name is required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "summary", "project.summary.required", "Project summary is required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "project.description.required", "Project description is required");

		if ((project.getServiceCategories() == null) || (project.getServiceCategories().size() == 0)) {
			errors.rejectValue("serviceCategories", "project.serviceCategories.required", "At least one service category is required");
		}

		super.validate(target, errors);
	}

}
